package com.dongdongshop.controller;

import com.dongdongshop.data.Result;
import com.dongdongshop.pojo.TbBrand;
import com.dongdongshop.pojo.TbTypeTemplate;
import com.dongdongshop.service.TypeTemplateService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@RequestMapping("/typeTemplateController")
@Controller
public class TypeTemplateController {

    private Logger logger = LoggerFactory.getLogger(TypeTemplateController.class);

    @DubboReference
    private TypeTemplateService typeTemplateService;
    @RequestMapping("/toShow")
    public String toShow (){
        return "/admin/type_template";
    }
    @RequestMapping("/listTypeTemplates")
    @ResponseBody
    public Result listTypeTemplates(String name){
        try {
            List<TbTypeTemplate> tbTypeTemplates = typeTemplateService.listTypeTemplates(name);
//            int i = 9/0;
            return Result.OK().setData(tbTypeTemplates);
        }catch (Exception e){
            logger.error("模板列表查询失败:{}",e);
            return Result.ER();
        }
    }
    @RequestMapping("/addTemplate")
    @ResponseBody
    public Result addTemplate(String name,String specIds,String brandIds,String customAttributeItems){
        try {

            typeTemplateService.addTemplate(name,specIds,brandIds,customAttributeItems);
            return Result.OK();
        }catch (Exception e){
            logger.error("模板列表添加失败:{}",e);
            return Result.ER();
        }
    }
    @RequestMapping("/delTemplates")
    @ResponseBody
    public Result delTemplates(@RequestParam(value = "ids") List<Long> ids){
        try {
            typeTemplateService.delTemplates(ids);
            return Result.OK();
        }catch (Exception e){
            logger.error("模板列表删除失败:{}",e);
            return Result.ER();
        }
    }
    @RequestMapping("/selectTemplateById")
    @ResponseBody
    public Result selectTemplateById(Long id){
        try {
            TbTypeTemplate tbTypeTemplate = typeTemplateService.selectTemplateById(id);
            return Result.OK().setData(tbTypeTemplate);
        }catch (Exception e){
            logger.error("模板列表删除失败:{}",e);
            return Result.ER();
        }
    }
    @RequestMapping("/updateTemplate")
    @ResponseBody
    public Result updateTemplate(String name,String specIds,String brandIds,String customAttributeItems,Long id){
        try {

            typeTemplateService.updateTemplate(name,specIds,brandIds,customAttributeItems,id);
            return Result.OK();
        }catch (Exception e){
            logger.error("模板列表添加失败:{}",e);
            return Result.ER();
        }
    }
}
